package interaction.sampling;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class NumericAnalyzer {
	private Sample values;
	private Double sum = null;
	private Double average = null;
	private Float minimum = null;
	private Float maximum = null;
	private Double standardDeviation = null;
	
	public NumericAnalyzer(Sample values) {
		this.values = values;
	}
	
	public double getSum()
	{
		if (sum == null) {
			sum = 0.0;
			for (Float value : values) {
				sum += value.doubleValue();
			}
		}
		return sum;
	}
	
	public double getAverage()
	{
		if (average == null) {
			average = getSum() / values.size();
		}
		return average;
	}
	
	public Float getMinimum()
	{
		if (minimum == null) {
			minimum = Collections.min(values); 
		}
		return minimum;
	}
	
	public Float getMaximum()
	{
		if (maximum == null) {
			maximum = Collections.max(values);
		}
		return maximum;
	}
	
	public double getStandardDeviation()
	{
		if (standardDeviation == null) {
			double average = getAverage();
			double deviation = 0;
			for (Float value : values) {
				deviation += Math.pow(value.doubleValue() - average, 2);
			}
			standardDeviation = Math.sqrt(deviation / values.size());
		}
		return standardDeviation;
	}
	
	public double getDeviationFromAverage(double value)
	{
		return Math.abs(getAverage() - value) / getStandardDeviation();
	}
	
	public Sample getDeviation(Sample from)
	{
		return Samples.deviation(values, from);
	}
}
